package com.wlz.kernel.netty.client;


import com.wlz.kernel.netty.hessian.HessianDecode;
import com.wlz.kernel.netty.hessian.HessianEncode;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;

import java.util.concurrent.CountDownLatch;

public class NettyClientHandlerInitializer extends ChannelInitializer<SocketChannel>{
    private CountDownLatch lathc;
    private NettyClientHandler handler;
    private LengthFieldPrepender fieldEncoder=new LengthFieldPrepender(2);

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        handler =  new NettyClientHandler(lathc);
        HessianEncode hessionEncodeHandler=new HessianEncode();
        HessianDecode hessionDecodeHandler= new HessianDecode();

        sc.pipeline().addLast(fieldEncoder);
        sc.pipeline().addLast(hessionEncodeHandler);

        //入站
        sc.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535,
                0, 2, 0, 2));
        sc.pipeline().addLast(hessionDecodeHandler);
        sc.pipeline().addLast(handler);


    }
    public Object getServerResult(){
        return handler.getResult();
    }

    public NettyClientHandlerInitializer(CountDownLatch lathc) {
        this.lathc = lathc;
    }

    public void reLathc(CountDownLatch lathc){
        this.handler.reLathc(lathc);
    }
}